package com.quanyan.operative.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.constants.Constants;
import com.quanyan.operative.db.TbOperateSubject;
import com.quanyan.operative.db.TbOperateSubjectActivity;
import com.quanyan.operative.db.TbOperateSubjectActivityExample;
import com.quanyan.operative.db.TbOperateSubjectExample;
import com.quanyan.operative.mapper.TbOperateSubjectActivityMapper;
import com.quanyan.operative.mapper.TbOperateSubjectMapper;
import com.quanyan.operative.model.req.ReqOperatingActivity;
import com.quanyan.operative.model.resp.RespOperatingActivityDetail;
import com.quanyan.operative.model.resp.RespOperatingActivitySubject;
import com.quanyan.operative.service.OperateActivityService;
import com.quanyan.place.client.PlaceClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 专题活动Service
 * Created by admin on 2016/11/22.
 */
@Service("operateActivityService")
public class OperateActivityServiceImpl implements OperateActivityService {
    private static Logger logger = LoggerFactory.getLogger(OperateActivityServiceImpl.class);

    @Autowired
    TbOperateSubjectMapper tbOperateSubjectMapper;

    @Autowired
    TbOperateSubjectActivityMapper subjectActivityMapper;

    @Autowired
    private PlaceClient placeClient;

    @Override
    public List<RespOperatingActivitySubject> getPlaceList(ReqOperatingActivity reqOperatingActivity) {
        return null;
    }

    /**
     * 获取专题及专题关联活动
     *
     * @param subjectId
     * @param cityId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public RespOperatingActivitySubject getSubjectAndActivity(Integer subjectId,
                                                              Integer cityId,
                                                              Integer pageNum,
                                                              Integer pageSize) {
        RespOperatingActivitySubject respSubjectAndActivity = new RespOperatingActivitySubject();
        TbOperateSubject subject = null;
        if (null != subjectId) {
            subject = tbOperateSubjectMapper.selectByPrimaryKey(subjectId);
        } else {
            TbOperateSubjectExample example = new TbOperateSubjectExample();
            TbOperateSubjectExample.Criteria criteria = example.createCriteria();
            criteria.andIsEnableEqualTo(Constants.BYTE_ONE); // 可用的
            criteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE); //  未被删除的
            List<TbOperateSubject> subjectList = tbOperateSubjectMapper.selectByExample(example);
            if (null != subjectList && subjectList.size() > 0) {
                subject = subjectList.get(0);
            }
        }

        if (null == subject || Constants.BYTE_ZERO == subject.getIsEnable()) {
            // 未发布的不展示
            return null;
        } else {
            respSubjectAndActivity.setSubjectId(subject.getId());
            respSubjectAndActivity.setSubjectName(subject.getName()); // 专题名称
            respSubjectAndActivity.setImageUrl(subject.getImageUrl()); // 图片
            respSubjectAndActivity.setSubjectDesc(subject.getSubName()); // 专题描述
            List<RespOperatingActivityDetail> subjectActivityList = getRespSubjectActivityList(subject.getId(), pageNum, pageSize);
            respSubjectAndActivity.setActivityList(subjectActivityList);
        }
        return respSubjectAndActivity;
    }

    /**
     * 根据专题id查询专题活动
     *
     * @param subjectId
     * @return
     */
    public List<TbOperateSubjectActivity> getSubjectActivityList(Integer subjectId, Integer pageNum, Integer pageSize) {
        TbOperateSubjectActivityExample example = new TbOperateSubjectActivityExample();
        TbOperateSubjectActivityExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.BYTE_ONE);
        if (null != subjectId){
            criteria.andSubjectIdEqualTo(subjectId);
        }
        int totalNum = subjectActivityMapper.countByExample(example);
        example.setPage(new Page(pageNum * pageSize, pageSize, totalNum));
        List<TbOperateSubjectActivity> result = subjectActivityMapper.selectByExample(example);
        return result;
    }

    /**
     * 根据专题id获取专题关联的活动
     *
     * @param subjectId
     * @return
     */
    public List<RespOperatingActivityDetail> getRespSubjectActivityList(Integer subjectId, Integer pageNum, Integer pageSize) {
        List<RespOperatingActivityDetail> result = new ArrayList<RespOperatingActivityDetail>();
        List<TbOperateSubjectActivity> respSubjectActivityList = this.getSubjectActivityList(subjectId, pageNum, pageSize);
        for (TbOperateSubjectActivity tbOperateSubjectActivity : respSubjectActivityList) {
            RespOperatingActivityDetail respOperatingActivityDetail = new RespOperatingActivityDetail();
            //活动ID
            respOperatingActivityDetail.setActivityId(tbOperateSubjectActivity.getActivityId());
            // 活动摘要
            respOperatingActivityDetail.setSummary(tbOperateSubjectActivity.getSummary());
            // 活动名称
            respOperatingActivityDetail.setActivityName(tbOperateSubjectActivity.getActivityName());
            // 图片
            respOperatingActivityDetail.setImageUrl(tbOperateSubjectActivity.getImageUrl());
            // 活动专题
            respOperatingActivityDetail.setExtendInfo(tbOperateSubjectActivity.getExtendInfo());
            result.add(respOperatingActivityDetail);
        }
        return result;
    }

    /**
     * 判断用户是否有订场行为
     * @param uid
     * @return
     */
    @Override
    public APIResponse isUserBookPlace(Integer uid) {
        boolean result;
        try {
            result = placeClient.isAnyOrder(uid);
        } catch (Exception e) {
            logger.error("服务器异常！：{}",e.getMessage());
            return APIResponse.returnFail("服务器异常");
        }
        return APIResponse.returnSuccess(result);
    }
}
